﻿using GBT_Tool;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using QueryDataLogic;
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Utility.QRCode_F4;
using SpeechLib;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;
using GBT_Tool_v4._0.ToolHelps;

namespace test
{
    public partial class Form1 : Form
    {
        BasicInfo bi = new BasicInfo();
        MealReport mr = new MealReport();
        UserVerify uv = new UserVerify();
        WXServiceAPI wxApi = new WXServiceAPI();
        HttpRequest hr = new HttpRequest();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //JObject jo = new JObject();

            //jo = bi.GetMeal();

            //richTextBox1.Text = jo.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //JObject jo = new JObject();

            //jo = bi.GetDiningHall();

            //richTextBox1.Text = jo.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //JObject jo = new JObject();

            //jo = mr.GetMealReport(10);

            //richTextBox1.Text = jo.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //JObject jo = new JObject();

            //jo = uv.SetInvitationCode();

            //richTextBox1.Text = jo.ToString();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = WXServiceAPI.AccessToken;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            JObject joDate = new JObject();
            joDate["value"] = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            JObject joTime = new JObject();
            joTime["value"] = DateTime.Now.ToString("yyyy-MM-dd") + " 17:30:00";
            JObject joPhrase = new JObject();
            joPhrase["value"] = "请及时报餐";

            JObject joData = new JObject();
            joData["date1"] = joDate;
            joData["time2"] = joTime;
            joData["phrase3"] = joPhrase;

            JObject jo = new JObject();
            jo["template_id"] = "1L5a_xdZW3jeggkOV7N03Mbrzzg4E4Ql9OE6ZHtY-xs";
            jo["page"] = "pages/authorization/authorization";
            jo["data"] = joData;
            jo["touser"] = "oJNvX5WadA-YRkjonfS8cwJcNbOw";
            //jo["touser"] = "oJNvX5aAZHGfs0cMnhi-ZCZbC0Ck";//郑剑文
            
            wxApi.sendMealMessage(jo);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                JObject param = new JObject();
                param["districtID"] = 1;
                string requestJson = JsonConvert.SerializeObject(param);

                //调用发送报餐提醒接口
                richTextBox1.Text = hr.GBTPost("https://wms.egbt.net:8081/gbt/MealMessage/SendMessage", requestJson);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //获取新冠状病毒疫情数据
        private void button8_Click(object sender, EventArgs e)
        {
            string result = "";
            result = hr.GBTGet("https://tianqiapi.com/api?version=epidemic&appid=14361395&appsecret=Ni554XPx");

            JObject joResult = JObject.Parse(result);

            if (string.IsNullOrEmpty(result))
            {
                label1.Text = "未获取到数据";
            }
            else
            {
                richTextBox1.Text = joResult.ToString();
            }
        }

        public static Bitmap GenerateBarcode(string text, int width, int height)
        {
            BarcodeWriter<Bitmap> writer = new BarcodeWriter<Bitmap>();
            //使用ITF 格式，不能被现在常用的支付宝、微信扫出来
            //如果想生成可识别的可以使用 CODE_128 格式
            //writer.Format = BarcodeFormat.ITF;
            writer.Format = BarcodeFormat.CODE_128;
            EncodingOptions options = new EncodingOptions()
            {
                Width = width,
                Height = height,
                Margin = 2
            };
            writer.Options = options;
            BitmapRenderer bitmapRenderer = new BitmapRenderer();
            writer.Renderer = bitmapRenderer;
            Bitmap bmp = writer.Write(text);
            return bmp;
        }

        public static Bitmap combineBitmap(Bitmap QRCodeBitmap, string qrdata)
        {
            Bitmap GBitmap = GenerateBarcode(qrdata, QRCodeBitmap.Width, 30);
            var newBitmap = new Bitmap(QRCodeBitmap.Width, QRCodeBitmap.Height + GBitmap.Height);
            for (var i = 0; i <= GBitmap.Width - 1; i++)
            {
                for (var j = 0; j <= GBitmap.Height - 1; j++)
                {
                    var c = GBitmap.GetPixel(i, j);
                    newBitmap.SetPixel(i, j, c);
                }
            }

            for (var i = 0; i <= QRCodeBitmap.Width - 1; i++)
            {
                for (var j = 0; j <= QRCodeBitmap.Height - 1; j++)
                {
                    var c = QRCodeBitmap.GetPixel(i, j);
                    newBitmap.SetPixel(i, j + GBitmap.Height, c);
                }
            }

            return newBitmap;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label2.Text = Guid.NewGuid().ToString().Replace("-","").ToUpper();

            QRCodeEncoder encoder = new QRCodeEncoder();
            encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;//编码方法(注意：BYTE能支持中文，ALPHA_NUMERIC扫描出来的都是数字)
            encoder.QRCodeScale = 4;//大小
            encoder.QRCodeVersion = 0;//版本(注意：设置为0主要是防止编码的字符串太长时发生错误)
            encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
            Tools tools = new Tools();
            string qrdata = "123";
            Bitmap bp = encoder.Encode(qrdata, Encoding.GetEncoding("UTF-8"));
            label1.Text = qrdata;
            bp = tools.GenerateBarcode(qrdata, (uint)80);

            pictureBox1.Image = bp;

            //SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
            //Db = new SqlSugarClient(new ConnectionConfig()
            //{
            //    ConnectionString = "server=115.29.65.173;uid=sa;pwd=Aa123456;database=MealSystem_Test",
            //    DbType = DbType.SqlServer,
            //    InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
            //    IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
            //});
            ////调式代码 用来打印SQL 
            //Db.Aop.OnLogExecuting = (sql, pars) =>
            //{
            //    Console.WriteLine(sql + "\r\n" +
            //        Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            //    Console.WriteLine();
            //};
            //UserVerify uv = new UserVerify();
            //label2.Text = uv.IsUser(2, "88050568012", Db).ToString();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            //CheckMeal cm = new CheckMeal();
            //JObject joParam = cm.GetParam("https://wms.egbt.net/meal?number=1&districtId=1");
            //richTextBox1.Text = joParam.ToString();
        }

        public void speak(string speechSounds)
        {
            SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            SpVoice spVoice = new SpVoice();
            //spVoice.Rate = spVoice.Rate - 5;
            //if (spVoice.Volume < 100)
            //{
            //    spVoice.Volume = spVoice.Volume + 10;
            //}

            //if (spVoice.Volume > 9)
            //{
            //    spVoice.Volume = spVoice.Volume - 10;
            //}
            spVoice.Speak(speechSounds, SpFlags);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            speak("成功领取一份餐");
        }
    }
}
